//
//  EmojiUtils.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/27.
//

import Foundation
class EmojiUtils {
    
    static var EMOJI_MAP2:[String:String] = [:]
    
    static func deleteNoEmotionContentAll(_ text: String?) -> String? {
        guard let text = text else {
            return nil
        }
        
        // 正则表达式模式（Swift 中不需要双重转义）
        let pattern = #"\[[^\]]*?\]|【[^】]*?】|<[^>]*?>|\([^()]*?\)|《[^》]*?》|（[^）]*?）"#
        
        do {
            let regex = try NSRegularExpression(pattern: pattern, options: [])
            let range = NSRange(location: 0, length: text.utf16.count)
            return regex.stringByReplacingMatches(
                in: text,
                options: [],
                range: range,
                withTemplate: ""
            )
        } catch {
            showLog("正则表达式错误: \(error)")
            return text
        }
    }
    
    /// 删除不匹配 {{}}
    static func deleteNoEmotionContent2(text: String?) -> String? {
        guard let text = text else {
            return text
        }
        
        // 查找 {{xxx}}
        let regexEmotion = "\\{\\{.*?\\}\\}"
        let patternEmotion = try? NSRegularExpression(pattern: regexEmotion, options: [])
        let matches = patternEmotion?.matches(in: text, options: [], range: NSRange(location: 0, length: text.count)) ?? []
        
        var list: [String] = []
        
        for match in matches {
            let range = Range(match.range, in: text)!
            let key = String(text[range])
            
            // 假设 EMOJI_MAP2 是一个 [String: Emoji] 字典
            let emoji = EmojiUtils.EMOJI_MAP2[key]
            if emoji == nil {
                list.append(key)
            }
        }
        
        // 将字符串转换为可变字符串进行替换操作
        var result = text
        for str in list {
            result = result.replacingOccurrences(of: str, with: "")
        }
        
        return result
    }
}
